package file;
import java.util.*;
import java.io.*;

import utility.Input;

public class FileOperation
{
	String Path;
	String sortedStringArray[];
	Hashtable<Object,Object> hashtableForUppend = new Hashtable<Object,Object>();	
	RandomAccessFile fwriter;

	public FileOperation(RandomAccessFile fwriter)
	{	
		this.fwriter = fwriter;
		
	}
	
	public FileOperation(String Path)
	{
		this.Path = Path;
		
	}

	public boolean addRecordToFile (Hashtable<Object,Object> hash)
	{	
		System.out.println ("hashtable enterd into add record method = "+hash);
		
		try
		{
			fwriter.seek(0);
			String line = fwriter.readLine();
			String []ArrayForSort = line.split(Input.SEPARATOR);
			for(int i=0;i<ArrayForSort.length;i++)
			{
				try 
				{
					ArrayForSort[i] = hash.get(ArrayForSort[i].toLowerCase()).toString();
				} catch (Exception e) 
				{	
					ArrayForSort[i] = "";
				}
				System.out.println(ArrayForSort[i]);
			}
			
			System.out.println("Appended record is :");
			//raf.seek(Path.length());
			fwriter.seek(fwriter.length());
			
			for(int i=0;i<ArrayForSort.length;i++)
			{				
				System.out.print(ArrayForSort[i]+Input.SEPARATOR);
				fwriter.writeBytes(ArrayForSort[i]+Input.SEPARATOR);
			}
			sortedStringArray = ArrayForSort;
			fwriter.writeBytes(Input.ENDSEPARATOR);
			return true;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}	
	
	synchronized public boolean updateRecord(long indexOfLine, Hashtable<Object, Object> hashtableForUppend)
	{
		try
		{
			fwriter.seek(0);
			// code for detecting the line number and placing cursor at particular line
			int s = hashtableForUppend.size();
			String ArrayForSort[] = new String[s];
			int i=0;
	
			String line = fwriter.readLine();
			ArrayForSort = line.split(Input.SEPARATOR);
			for(i=0;i<ArrayForSort.length;i++)
			{
				try 
				{
					ArrayForSort[i] = hashtableForUppend.get(ArrayForSort[i].toLowerCase()).toString();
				} catch (Exception e) {
					ArrayForSort[i] = "";
				}
				System.out.println(ArrayForSort[i]);
			}
			
			System.out.println("Update record is :");
			//---------------------------------
			
			int countLine =1;
			StringBuffer data = new StringBuffer();
			data.append(line+Input.ENDSEPARATOR);
			while ((line = fwriter.readLine()) != null)
			{
				countLine++;
				//long afterLine = raf.getFilePointer();
				data.append(line+"\n");
				if (countLine == (indexOfLine-1))
				{
					line = fwriter.readLine();
					String [] tmpArray = line.split(Input.SEPARATOR);
					for(i=0;i<ArrayForSort.length;i++)
					{				
						
						try
						{
							if(ArrayForSort[i].length()!=0)
							{
								System.out.print(ArrayForSort[i]+"\t");
								data.append(ArrayForSort[i]+"\t");
							}
							else
							{
								System.out.print(tmpArray[i]+"\t");
								data.append(tmpArray[i]+"\t");
							}
						} catch (Exception e) {
							data.append(""+"\t");
						}
					}
					data.append("\n");
					countLine++;
				}
			}
			fwriter.seek(0);
			System.out.println("\n"+data.toString());
			fwriter.writeBytes(data.toString());
			fwriter.setLength(data.length());
			return true;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
		}
	synchronized public boolean updateRecord(Hashtable<Object, Object> hashtableForUppend, Hashtable<Object,Object>Condition)
	{
		boolean FLAG=false;
		//Search For Record
		try
		{
			int counter=1;
			
			FileUtility fu = new FileUtility(fwriter);
			fwriter.seek(0);
			String line=fwriter.readLine();
			
			StringBuffer data = new StringBuffer();
			data.append(line+Input.ENDSEPARATOR);
			
			String [] ArrayForSort = line.split(Input.SEPARATOR);
			for(int i=0;i<ArrayForSort.length;i++)
			{
				try 
				{
					ArrayForSort[i] = hashtableForUppend.get(ArrayForSort[i]).toString();
				} catch (Exception e) {
					ArrayForSort[i] = "";
				}
//				System.out.println(ArrayForSort[i]);
			}
			
			while((line=fwriter.readLine())!=null)
			{
				counter++;
				System.out.println("The Line is : " +line);
				String[]arr2=line.split(Input.SEPARATOR);
				Enumeration<Object> hmIterator = Condition.keys();
				try 
				{
					FLAG = false;
					while (hmIterator.hasMoreElements())
					{
						String clmName = (String) hmIterator.nextElement();
						int colindex = fu.getColumnIndex(clmName);
						String valOfHash = Condition.get(clmName).toString();
						try {
							if (arr2[colindex-1].equals(valOfHash))
							{
								FLAG = true;
							}
							else if(Double.parseDouble(arr2[colindex-1])==Double.parseDouble((valOfHash)))
							{
								FLAG = true;
							}
							else
							{
								FLAG = false;
								break;
							}
						} 
						catch (Exception e) 
						{
							FLAG = false;
						}
					}
					if (FLAG == true)
					{
						System.out.println("The Record Index is : "+counter);
						//Update Record
						
						String [] tmpArray = line.split(Input.SEPARATOR);
						for(int i=0;i<ArrayForSort.length;i++)
						{				
							
							try
							{
								if(ArrayForSort[i].length()!=0)
								{
									System.out.print(ArrayForSort[i]+Input.SEPARATOR);
									data.append(ArrayForSort[i]+Input.SEPARATOR);
								}
								else
								{
									System.out.print(tmpArray[i]+Input.SEPARATOR);
									data.append(tmpArray[i]+Input.SEPARATOR);
								}
							} catch (Exception e) {
								data.append(""+Input.SEPARATOR);
							}
						}
						data.append(Input.ENDSEPARATOR);
					}
					else
						data.append(line+Input.ENDSEPARATOR);
				}
				
				catch (Exception e) 
				{
					data.append(line+Input.ENDSEPARATOR);
					e.printStackTrace();
				}
			}
			fwriter.seek(0);
			System.out.println(Input.SEPARATOR+data.toString());
			fwriter.writeBytes(data.toString());
			fwriter.setLength(data.length());
			return true;
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteFileRecord(Hashtable<Object,Object>Condition)
	{
		boolean RESULT = false;
		boolean FLAG=false;
		//Search For Record
		try
		{
			int counter=1;
			
			FileUtility fu = new FileUtility(fwriter);
			fwriter.seek(0);
			String line=fwriter.readLine();
			
			StringBuffer data = new StringBuffer();
			data.append(line+Input.ENDSEPARATOR);
			
			while((line=fwriter.readLine())!=null)
			{
				counter++;
				System.out.println("The Line is : " +line);
				String[]arr2=line.split(Input.SEPARATOR);
				Enumeration<Object> hmIterator = Condition.keys();
				try 
				{
					while (hmIterator.hasMoreElements())
					{
						try 
						{
							String clmName = (String) hmIterator.nextElement();
							int colindex = fu.getColumnIndex(clmName);
							String valOfHash = Condition.get(clmName).toString();
							if (arr2[colindex-1].equals(valOfHash))
							{
								FLAG = true;
							}
							else if(Double.parseDouble(arr2[colindex-1])==Double.parseDouble((valOfHash)))
							{
								FLAG = true;
							}
							else
							{
								FLAG = false;
								break;
							}
						} 
						catch (Exception e) 
						{
							FLAG = false;

						}
					}
					if (FLAG == true)
					{
						System.out.println("Match Found And Record is deleted");
						RESULT = true;
					}
					else
					{
						data.append(line+Input.ENDSEPARATOR);
					}
				}
				catch (Exception e) 
				{

				}
			}
			if(RESULT)
			{
				fwriter.seek(0);
				fwriter.writeBytes(data.toString());
				fwriter.setLength(data.length());
				return RESULT;
			}
			
		}
		catch (Exception e) {
		
		}
		return false;
	}
	
	public boolean deleteFileRecord(long indexOfLine)
	{
		try
		{	
			fwriter.seek(0);
			
			StringBuffer txt= new StringBuffer();
			int c;
			int countLine =1;
			while (fwriter.read() != -1)
			{
				fwriter.seek(fwriter.getFilePointer()-1);
				while (fwriter.read() != '\n');
				{
					fwriter.read();			
					countLine++;
					long afterLine = fwriter.getFilePointer();
					//System.out.println (afterLine);
					//System.out.println (countLine);
					if (countLine == indexOfLine)
					{
						//System.out.println ("match found");
						while (fwriter.read() != '\n');
						
						while ((c=fwriter.read()) != -1){
							txt.append((char)c);
							//System.out.println (txt.toString());
						}
						System.out.println (txt.toString());
						fwriter.seek(afterLine-1);
						fwriter.writeBytes(txt.toString());
						fwriter.setLength(fwriter.getFilePointer());
					}
				}
			}
			return true;
		}				// close try
		catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}	
	
public static void main(String[] args) {
		
		RandomAccessFile fwriter = null;
		try
		{//Specify the perticular path of a file 
			fwriter = new RandomAccessFile("/home/sachin/Reports/Basic_Reports/Master_Reports/4203/4203_mid.txt","rws");
		} catch (FileNotFoundException e) {
		}
		FileOperation fo = new FileOperation (fwriter);
		Hashtable<Object, Object> hashtableForUppend = new Hashtable<Object, Object>();
		Hashtable<Object, Object> Condition = new Hashtable<Object, Object>();
		Condition.put("pid", "111");
		hashtableForUppend.put("pid", "9");

		/*Hashtable<Object, Object> addRecord = new Hashtable<Object, Object>();
		addRecord.put("pid", "222");
		fo.addRecordToFile(addRecord);*/
		
		Hashtable<Object, Object> deleteRecord= new Hashtable<Object, Object>();
		deleteRecord.put("pid", "111");
		fo.deleteFileRecord(Condition);
		
	/*	Hashtable<Object, Object> updateRecord = new Hashtable<Object, Object>();
		updateRecord.put("pid", "10");
		fo.updateRecord(updateRecord, Condition);*/	
	
	}
}